# -*- coding:utf-8 -*-
import pymongo
import json
import requests
import re

class MongoUtil():

    def __int__(self):
        self.address = "mongodb://10.47.0.82:27017/"
    # 连接mongodb，获取食物名称和url
    def connect(self):
        myclient = pymongo.MongoClient("mongodb://10.47.0.82:27017/")
        db = myclient["recommend_diet"]
        col = db["xiachufang_menu"]
        # 引入类
        czt = clean_text()

        #x = col.find()

        #for x in col.find({},{"_id": 0,'name':1,'main_img':1}):
        for x in col.find({}, {"_id": 0, 'name':1,'main_img': 1}):

            print(x)
            print(type(x))
            # 清洗菜单
            x['name'] = czt.clean_zh_text(x['name'])
            print(x['name'])
            # #  字符串转成字典对象
            # # x=json.loads(x)
            #
            # # 清洗菜名  去掉\n
            # if '\n' in x['name']:
            #     x['name']=x['name'].split('\n')[0]
            #
            # # 清洗菜名 / 去掉
            #
            # if '/' in x['name']:
            #     food_name1 = x['name'].split('/')[0]
            #     food_name2 = x['name'].split('/')[1]
            #     print(food_name1)
            #     print(food_name2)
            #
            #     # 字典转换成字符串
            #     # food_name1 = str(x['name1'])
            #     # food_name2 = str(x['name2'])
            #     # food_name = food_name1 + food_name2
            #     #x['name'] = eval(food_name1+food_name2)
            #     x['name'] = food_name1 + food_name2
            #
            # # 清洗菜名|去掉
            # if '丨'in x['name']:
            #     x['name'] = x['name'].split('丨')[0]
            #
            #
            # if'|' in x['name']:
            #     x['name'] = x['name'].split('|')[0]
            #
            #
            #
            # # 删除菜名中的（）
            # x['name'] =re.sub('（.*?）',"",x['name'])
            # print(x['name'])
            #
            # # 清洗菜名<>去掉
            # x['name'] = re.sub('<.*?>',"",x['name'])
            # print(x['name'])
            #
            # # 清洗去掉菜名中的【】
            # x['name'] = re.sub('【.*?】',"",x['name'])
            # print(x['name'])
            #
            # #删除菜名中的\\
            # x['name'] = re.sub('\\\.*?\\\'',"",x['name'])
            # print(x['name'])
            # # 删除菜名中的:
            # x['name'] = re.sub(':.*?', "", x['name'])
            # print(x['name'])
            # # 删除菜名中的“
            # x['name']=re.sub('“.*?"',"",x['name'])
            # print(x['name'])
            #
            # # # 删除菜名中的《》
            x['name']=re.sub('re.compile("[^A-Z^a-z^0-9^\u4e00-\u9fa5]")',x['name'])
            print(x['name'])




            r = requests.get(x['main_img'], stream = True)
            print(r.status_code) # 返回状态码
            if r.status_code == 200:
                # 将内容保存到图片
                open('E:\\pic\\{}.png'.format(x['name']),
                     'wb').write(r.content)
                print("done")
                del r




        ai_count = col.find().count()
        print(ai_count)
        # 获取的名称保存到本地
        # with open('ai_food.json','a') as f:
        #     data = list()
        #     for i in col.find({},{"_id": 0,'name':1}):
        #         data.append(i)
        #
        #     f.write(json.dumps(data,ensure_ascii=False))

    # make Chinese text clean
class clean_text():

    def clean_zh_text(self,text):
        # keep English, digital and Chinese
        comp = re.compile('[^A-Z^a-z^0-9^\u4e00-\u9fa5]')
        return comp.sub('', text)





if __name__ == '__main__':
    m = MongoUtil()
    m.connect()